package com.njxzc.igo.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.njxzc.igo.model.User;
@WebFilter(servletNames={"LoadAllOrders","LoadOrderTypes","modifyOrderType","addproductType",
		"AddNewProducts","DeleteProduct","loadproductstypes","addnewProducts","Aloadalluserinfo",
		"modifyPassword","registAdmin","ALoadUserTypes","modifyUserTypes"},urlPatterns="/manage.jsp")
public class Filter2_AdminIdentifyFilter implements Filter {
	@Override
	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
			throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) resp;
		HttpSession session = request.getSession();

		User u = (User) session.getAttribute("u");
		if (u == null) {
			request.getRequestDispatcher("errorpage/404.jsp").forward(request, response);
			return;
		}
		if (session.getAttribute("u") != null
				&& (u.getUsertype().getTypeId() != 1 && u.getUsertype().getTypeId() != 4)) {
			request.getRequestDispatcher("errorpage/404.jsp").forward(request, response);
			return;
		}

		chain.doFilter(req, resp);
	}

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub

	}
}
